using FrameWork;
using UnityEngine;

public class UIInstance : ObjectBase
{
    private UILogic _dialogLogic;

    public UILogic DialogLogic
    {
        get => _dialogLogic;
    }

    /// <summary>
    /// 界面id
    /// </summary>
    private int _dialogId;

    public int DialogId
    {
        get => _dialogId;
    }

    public GameObject GameObject
    {
        get => GetDialog().gameObject;
    }

    protected internal override void OnSpawn()
    {
        _dialogLogic.SetVisible(true);
    }

    protected internal override void OnUnSpawn()
    {
        _dialogLogic.SetVisible(false);
    }

    protected internal override void Release()
    {
        GameObject.Destroy(_dialogLogic.gameObject);
        Clear();
    }

    /// <summary>
    /// 获取界面
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T GetDialog<T>() where T:BaseDialog
    {
        BaseDialog dialog = _dialogLogic.Dialog;
        return dialog as T;
    }

    public BaseDialog GetDialog()
    {
        return _dialogLogic.Dialog;
    }

    /// <summary>
    /// 激活UIInstance
    /// </summary>
    public void Active()
    {
        Init(_name, DialogLogic);
    }

    public static UIInstance CreateInstance(int serializeId, UILogic logic,string poolName)
    {
        var uiInstance = ReferencePool.Acquire<UIInstance>();
        uiInstance._dialogLogic = logic;
        uiInstance._dialogId = serializeId;
        uiInstance.Init(poolName, logic);
        return uiInstance;
    }

    public void Update(float deltaTime)
    {
        _dialogLogic.OnUpdate(deltaTime);
    }
}
